<script>
import utils from "./utils/index";
import keypress from "./utils/keypress";

import allApi from "./api/index";
import { useWebSocketStore } from "./store/websocket";
import { useCommonStore } from "./store/common";
export default {
  onLaunch: function () {
    console.log("App Launch");
    // #ifdef APP-PLUS

    // 获取设备信息
    useCommonStore().setDeviceInfo();
    const snNo = useCommonStore().snNo;
    // const snNo = 'DP03P47R11921'
    snNo &&
      allApi.ecm.posDevice.getDeviceStatus(snNo).then((res) => {
        console.log("snInfo", res);
        if (!res.data || !res.data.id) {
          // 设备未绑定激活
          utils.toUrl("/pages/bind/index", "reLaunch");
        } else {
          useCommonStore().setPosId(res.data.id);
          useCommonStore().setPosNo(res.data.posNo);
          useCommonStore().setSnNo(res.data.snNo);
          useCommonStore().setAuthId(res.data.authId);
          useCommonStore().setMerchantId(res.data.merchantId);
          useCommonStore().setMerchantName(res.data.merchantName);
          // utils.toUrl("/pages/index/index", "reLaunch");
        }
      });
    // #endif
  },
  onShow: async function () {
    console.log("App Show");
    // 打开副屏
    await utils.openSecondScreen();
    // 获取副屏轮播图
    useCommonStore().getSecondConfig();
    // 获取收银机配置
    useCommonStore().getPosConfig();

    let posConfig = useCommonStore().posConfig || {};
    let swiper = useCommonStore().secondSwiper[0] || {};
    setTimeout(() => {
      utils.sendDataToSecondScreen({
        type: "setting",
        data: posConfig,
      });
      setTimeout(() => {
        // 发送轮播图数据
        utils.sendDataToSecondScreen({
          type: "swiper",
          data: swiper,
        });
      }, 1000);
    }, 1000);

    // 监听扫码枪事件
    // #ifdef APP-PLUS
    plus.key.addEventListener("keyup", keypress);
    // 连接钱箱
    utils.connectQianxiang();
    // 连接打印机
    utils.onConnect();

    // 发送在线状态
    useWebSocketStore().onConnectSend();
    // #endif
  },
  onHide: function () {
    console.log("App Hide");

    // 关闭键盘监听
    // #ifdef APP-PLUS
    plus.key.removeEventListener("keyup", keypress);
    // #endif
  },
  onExit() {
    // 关闭websocket
    useWebSocketStore().closeSocket();
  },
};
</script>

<style>
@import "@/static/css/iconfont.css";
</style>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "uview-plus/index.scss";
// 自定义公共样式
@import "@/static/css/common.scss";
#app {
  font-size: 16rpx;
  font-weight: 500;
  color: $uni-text-color-2;
  line-height: 1.3;
}
</style>
